package org.elasticsearch.script;

import org.elasticsearch.script.ScriptContext;
import org.elasticsearch.script.ScriptEngine;
import org.elasticsearch.script.SearchScript;

import java.util.Map;
/**
 * @author shenfl
 * ScriptEngine是统一的接口，而Script有各种SearchScript、SimilarityScript、TemplateScript等
 * 每种scritp最终cast成对用的script中的Factory接口
 */
public class PositionRecipScriptEngine implements ScriptEngine {
    @Override
    public String getType() {
        return "grouping_mixup_scripts";
    }

    @Override
    public <T> T compile(String name, String code, ScriptContext<T> context, Map<String, String> params) {
        if (!context.equals(SearchScript.CONTEXT)) {
            throw new IllegalArgumentException(getType() + " scripts cannot be used for context [" + context.name + "]");
        }
        // we use the script "source" as the script identifier
        if ("position_recip".equals(code)) {
            SearchScript.Factory factory = new PositionRecipScript.PositionRecipFactory();
            return context.factoryClazz.cast(factory);
        }
        throw new IllegalArgumentException("Unknown script name [" + code + "]");
    }
}